/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.knet.sp.ncrm.modules.customer.potentialadmingroup.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.knet.sp.ncrm.common.persistence.AjaxMessage;
import cn.knet.sp.ncrm.common.service.ServiceException;
import cn.knet.sp.ncrm.common.utils.StringUtils;
import cn.knet.sp.ncrm.common.web.BaseController;
import cn.knet.sp.ncrm.modules.customer.potentialadmingroup.entity.PotentialAdminGroup;
import cn.knet.sp.ncrm.modules.customer.potentialadmingroup.service.PotentialAdminGroupService;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 管理员分组Controller
 * @author 葛海洋
 * @version 2015-08-25
 */
@Controller
@RequestMapping(value = "${adminPath}/customer/potentialAdminGroup")
public class PotentialAdminGroupController extends BaseController {

	@Autowired
	private PotentialAdminGroupService potentialAdminGroupService;
	
	@ModelAttribute
	public PotentialAdminGroup get(@RequestParam(required=false) String id) {
		PotentialAdminGroup entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = potentialAdminGroupService.get(id);
		}
		if (entity == null){
			entity = new PotentialAdminGroup();
		}
		return entity;
	}
	
	/**
	 *  跳转列表
	 * @author Gehaiyang
	 * @date 2015年8月28日 下午2:52:33
	 * @param potentialAdminGroup
	 * @param model
	 * @return
	 */
	@RequiresPermissions("customer:potentialAdminGroup:view")
	@RequestMapping(value = {"list", ""})
	public String list(PotentialAdminGroup potentialAdminGroup,Model model) {
		return "modules/customer/potentialadmingroup/potentialAdminGroupList"; 
	}
	
	/**
	 *  获取列表数据
	 * @author Gehaiyang
	 * @date 2015年8月28日 下午2:52:43
	 * @param potentialAdminGroup
	 * @return
	 */
	@RequiresPermissions("customer:potentialAdminGroup:view")
	@RequestMapping(value = {"listAjaxData"})
	@ResponseBody
	public List<PotentialAdminGroup> listAjaxData(PotentialAdminGroup potentialAdminGroup) {
		return potentialAdminGroupService.findList(potentialAdminGroup); 
	}
	
	/**
	 *  添加
	 * @author Gehaiyang
	 * @date 2015年8月28日 下午2:53:04
	 * @param potentialAdminGroup
	 * @param model
	 * @return
	 */
	@RequiresPermissions("customer:potentialAdminGroup:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public AjaxMessage save(PotentialAdminGroup potentialAdminGroup, Model model) {
		AjaxMessage ajaxMessage = AjaxMessage.success();
		try {
			String groupName = potentialAdminGroup.getName();
			if (StringUtils.isBlank(groupName)) {
				ajaxMessage.makeError("分组名称不能为空");
				return ajaxMessage;
			}
			potentialAdminGroup.setName(StringUtils.trim(groupName));
			potentialAdminGroupService.save(potentialAdminGroup);
			ajaxMessage.setMessage("添加分组成功");
		} catch (ServiceException se) {
			logger.error(se.getMessage(),se);
			ajaxMessage.makeError(se.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			ajaxMessage.makeError("添加分组异常，请稍后重试");
		}
		return ajaxMessage;
	}
	
	/**
	 * 
	 *  修改分组
	 * @author Gehaiyang
	 * @date 2015年9月15日 上午9:42:53
	 * @param potentialAdminGroup
	 * @return
	 */
	@RequiresPermissions("customer:potentialAdminGroup:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public AjaxMessage delete(PotentialAdminGroup potentialAdminGroup) {
		AjaxMessage ajaxMessage = AjaxMessage.success();
		try {
			if (StringUtils.isNotBlank(potentialAdminGroup.getId())){ 
				potentialAdminGroupService.delete(potentialAdminGroup);
				ajaxMessage.setMessage("删除分组成功");
			} else {
				ajaxMessage.makeError("删除分组失败");
			}
			
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			ajaxMessage.makeError("删除分组异常，请稍后重试");
		}
		return ajaxMessage;
	}
	
	@RequiresPermissions("customer:potentialAdminGroup:edit")
	@RequestMapping(value = "update")
	@ResponseBody
	public AjaxMessage update(PotentialAdminGroup potentialAdminGroup, Model model) {
		AjaxMessage ajaxMessage = AjaxMessage.success();
		try {
			if (StringUtils.isBlank(potentialAdminGroup.getName())) {
				ajaxMessage.makeError("分组名称不能为空");
				return ajaxMessage;
			}
			potentialAdminGroupService.save(potentialAdminGroup);
			ajaxMessage.setMessage("修改分组成功");
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			ajaxMessage.makeError("修改分组异常，请稍后重试");
		}
		return ajaxMessage;
	}
	
	/**
	 *  获取分组树结构数据
	 * @author Gehaiyang
	 * @date 2015年8月28日 下午2:53:47
	 * @param extId
	 * @param response
	 * @return
	 */
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<PotentialAdminGroup> list = potentialAdminGroupService.findList(new PotentialAdminGroup());
		for (int i=0; i<list.size(); i++){
			PotentialAdminGroup e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return mapList;
	}
	
}